<template>
    <div>
        <iview-table
                ref="table"
                :baseURL="queryModel.baseURL"
                :menuId="dataModel.menuId"
                :columns="dataModel.columns"
                :order="dataModel.order"
                :pageNumber="pageModel.pageNumber"
                :pageSize="pageModel.pageSize"
                :defaultFilters="queryModel.defaultFilters"
                v-on="{changeTotalSize:changeTotalSize,events:events,changeSelect:changeSelect,expandRow:expandRow}">
        </iview-table>
        <br>
        <iview-page
                style="margin-bottom: 200px"
                :totalSize="pageModel.totalSize"
                :pageNumber="pageModel.pageNumber"
                :pageSize="pageModel.pageSize"
                :pageSizeOpts="pageModel.pageSizeOpts"
                v-on="{changePageNumber:changePageNumber,changePageSize:changePageSize}">
        </iview-page>
    </div>
</template>

<script>
    import iviewTable from '@/view/common/table/Table.vue';
    import iviewPage from '@/view/common/page/Page.vue';
    import expandRow from '@/view/business/medical/expand/courseExpand.vue';
    export default {
        components: {
            iviewTable,
            iviewPage,
            expandRow
        },
        data () {
            return {
                details: [],
                selectRow: {},
                selectRows: [],
                baseURL: '/user/treat/plan/course',
                detailURL: '/user/treat/plan/course/details',
                queryModel: {
                    // 网关代理路径(必须)+资源路径(可选)
                    baseURL: '/user/treat/plan/course',
                    defaultFilters: [
                        {'field': 'complaint_id', 'value1': '-1', 'operate': '=='}
                    ]
                },
                dataModel: {
                    menuId: 675,
                    columns: [
                        {
                            type: 'expand',
                            width: 50,
                            render: (h, params) => {
                                var vm = this
                                return h(expandRow, {
                                    props: {
                                        row: params.row,
                                        details: vm.details
                                    }
                                })
                            }
                        },
                        {
                            title: '添加时间',
                            key: 'creationTime',
                            sortable: true,
                            render: (h, params) => {
                                return h('span', this.$moment(params.row.creationTime).format('YYYY年MM月DD日'))
                            }
                        },
                        {
                            title: '疗程名称',
                            key: 'courseName',
                        },
                        {
                            title: '购买数量',
                            key: 'quantity',
                        },
                        {
                            title: '收费状态',
                            key: 'chargeStatus',
                            render: (h, params) => {
                                let temp = params.row.chargeStatus
                                if (temp === 0) {
                                    return h('div', [
                                        h('Tag', {
                                            props: {
                                                type: 'border',
                                                color: 'yellow'
                                            },
                                            style: {
                                            }
                                        }, '待交费')
                                    ]);
                                } else if (temp === 1) {
                                    return h('div', [
                                        h('Tag', {
                                            props: {
                                                type: 'border',
                                                color: 'green'
                                            },
                                            style: {
                                            }
                                        }, '已交费')
                                    ]);
                                }
                            }
                        },
                        {
                            title: '操作',
                            width: 250,
                            align: 'center',
                            render: (h, params) => {
                                return h('div', [
                                    h('Button', {
                                        props: {
                                            type: 'error',
                                            size: 'default'
                                        },
                                        style: {
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: () => {
                                                this.delete(params.row.id)
                                            }
                                        }
                                    }, '删除')
                                ]);
                            }
                        }
                    ],
                    order: 'id desc'
                },
                pageModel: {
                    totalSize: 0,
                    pageNumber: 1,
                    pageSize: 10,
                    pageSizeOpts: [5, 10, 20, 50, 100]
                }
            };
        },
        methods: {
            events (item) {
                let path = item.path
                if (path === 'add') {
                    this.add()
                }
            },
            add () {
                this.$emit('addCourse')
            },
            delete (id) {
                this.$Modal.confirm({
                    title: '操作提示',
                    content: '<p>确认删除当前数据?</p>',
                    onOk: () => {
                        this.doDelete(id)
                    }
                })
            },
            doDelete (id) {
                let url = this.baseURL + '/deleteById/' + id
                this.$ajax.post(url).then((response) => {
                    let {code, msg} = response.data
                    if (code === 0) {
                        this.$bus.alert('success', msg)
                        this.refresh()
                    } else {
                        this.$bus.alert('warning', msg)
                    }
                })
            },
            refresh () {
                this.queryModel.defaultFilters[0] = {'field': 'complaint_id', 'value1': this.$store.state.app.complaintId, 'operate': '=='}
                this.$refs.table.refresh()
            },
            changeSelect (rows) {
                this.selectRows = rows
            },
            expandRow (row, status) {
                if (status) {
                    let url = this.detailURL + '/findByCourseId/' + row.id
                    this.$ajax.get(url).then(r => {
                        let {code, data, msg} = r.data
                        if (code === 0) {
                            this.details = data
                        } else {
                            this.$bus.alert('error', msg)
                        }
                    })
                }
            },
            changeTotalSize (totalSize) {
                this.pageModel.totalSize = totalSize
            },
            changePageNumber (pageNumber) {
                this.pageModel.pageNumber = pageNumber
            },
            changePageSize (pageSize) {
                this.pageModel.pageSize = pageSize
            }
        }
    }
</script>

<style>

</style>
